<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="名称" prop="project_name">
        <el-input
          v-model="queryParams.project_name"
          placeholder="名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
        >添加
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      border
      default-expand-all
    >
      <el-table-column label="名称(项目或委托人)" align="center" prop="label" />
      <el-table-column label="员工" align="center" prop="staff" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            v-if="scope.row.pid == 0"
            size="mini"
            type="warning"
            @click="handleAdd(scope.row,scope.row.id)"
          >添加子项目</el-button>
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :form="form" @getList="getList" />
  </div>

</template>
<script>
import { projectList, projectDel } from '@/api/storehouse'
import AddEdit from './AddEdit'
export default {
  name: 'Index',
  components: {
    AddEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      info: '',
      // 查询参数
      queryParams: {
        project_name: undefined
      },
      form: {},
      show: false,
      ids: [],
      multiple: false

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      projectList(this.queryParams).then(response => {
        this.list = response.data
        this.total = response.count
        this.loading = false
      })
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.form = {}
    //   this.open = true
    //   this.title = '添加'
    // },
    handleAdd(scope = {}, form_id = 0) {
      this.form = { pid: form_id }
      this.open = true
      this.title = '添加'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      var employees = []
      if (row.employees) {
        var data = row.employees.split(',')
        data.forEach((item) => {
          employees.unshift(parseInt(item))
        })
      }
      // if (row.children) {
      //   this.form.project_name = row.children[0].label
      // } else {
      //   this.form.project_name = row.label
      // }
      this.form = {
        id: row.id,
        project_name: row.label,
        pid: row.pid,
        tenant_id: row.tenant_id,
        describe: row.describe,
        employees: employees,
        children: row.children
      }
      // console.log(row, employees, this.form)
      // return
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('是否删除该项目或委托人,如果该项目或委托人底下还有数据则一起删除,确定吗？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        projectDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        project_name: undefined
      }
      this.handleQuery()
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    }
  }
}
</script>

